package com.sltx.controller;

import com.sltx.controller.base.BaseController;
import com.sltx.service.QyqjService;
import com.sltx.service.ZsglService;
import com.sltx.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/zsgl")
public class ZsglController extends BaseController {
    @Autowired
    private ZsglService zsglService;

    @Autowired
    private QyqjService qyqjService;


    /**
     * 信息匹配
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getXxpp", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<Object> getXxpp(HttpServletRequest request) throws Exception {
        List<Object> result = new ArrayList<>();
        PageData pd = this.getPageData();
        result = qyqjService.getXxpp(pd);
        return result;
    }

    /**
     * 获取引进企业列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getYjqyList")
    @ResponseBody
    public Map<String, Object> getYjqyList(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        PageData pd = this.getPageData();

        int pageSize = Integer.parseInt(pd.get("pageSize").toString());
        int pageNo = Integer.parseInt(pd.get("pageNo").toString());
        Integer startPage = ((pageSize * (pageNo - 1)));
        Integer endPage = startPage + pageSize;

        pd.put("startPage", startPage.toString());


        result.put("num", zsglService.getNum());
        result.put("list", zsglService.getYjqyList(pd));

        return result;
    }

    /**
     * 根据关键字获取引进企业列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getYjqyByName")
    @ResponseBody
    public Object getYjqyByName(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        PageData pd = this.getPageData();

        int pageSize = Integer.parseInt(pd.get("pageSize").toString());
        int pageNo = Integer.parseInt(pd.get("pageNo").toString());

        Integer startPage = ((pageSize * (pageNo - 1)));
        Integer endPage = startPage + pageSize;

        pd.put("startPage", startPage.toString());
        pd.put("endPage", endPage.toString());

        if ("企业类型".equals(pd.get("ent_gjj"))) {
            pd.remove("ent_gjj");
        }
        if ("重点企业".equals(pd.get("ent_gjj"))) {
            pd.remove("ent_gjj");
            pd.put("ent_zdqy", "重点企业");
        }
        if ("外资企业".equals(pd.get("ent_gjj"))) {
            pd.remove("ent_gjj");
            pd.put("ent_sfwz", "是");
        }
        if ("信息提供单位".equals(pd.get("ent_xxtgdw"))) {
            pd.remove("ent_xxtgdw");
        }
        if ("企业引进单位".equals(pd.get("ent_yjqydw"))) {
            pd.remove("ent_yjqydw");
        }
        if ("认定状态".equals(pd.get("ent_state"))) {
            pd.remove("ent_state");
        }
        if ("".equals(pd.get("entname"))) {
            pd.remove("entname");
        }
        result.put("num", zsglService.getYjqyByNameNum(pd));
        result.put("list", zsglService.getYjqyByName(pd));

        return result;

    }

    /**
     * 获取企业详情
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getByName")
    @ResponseBody
    public Object getByName(HttpServletRequest request) {
        Map<Object, Object> result = new HashMap<Object, Object>();
        PageData pd = this.getPageData();
        List<Map<Object, Object>> byName = zsglService.getByName(pd);

        List<Object> zdqy = new ArrayList<>();
        for (Map m : byName) {
            if (m.get("ent_zdqy_xl") != null) {
                String s = m.get("ent_zdqy_xl").toString();
                // 判断字符串是否包含 ","
                if (s.contains(",")) {
                    String[] split = s.split(",");
                    for (int i = 0; i < split.length; i++) {
                        zdqy.add(split[i]);
                    }
                } else {
                    zdqy.add(m.get("ent_zdqy_xl").toString());
                }
            }
        }

        byName.get(0).put("ent_zdqy_xl", zdqy);
        result.put("qiye", byName);
        return result;
    }


    /**
     * 添加企业
     *
     * @param request
     * @return
     */
    @RequestMapping("/insertSave")
    @ResponseBody
    public Object insertSave(HttpServletRequest request) throws Exception {
        PageData pd = this.getPageData();
        pd.put("ent_state", "待认定");

        // 获取认定表数据并拼接
        String[] parameterValues = request.getParameterValues("rdbimg[]");
        String rdb = "";
        for (int i = 0; i < parameterValues.length; i++) {
            if (i == (parameterValues.length - 1)) {
                rdb += parameterValues[i];
            } else {
                rdb += parameterValues[i] + ",";
            }
        }
        pd.put("rdbimg", rdb);

        // 获取重点企业类别并拼接
        if (request.getParameterValues("ent_zdqy_xl[]") != null) {
            String[] ent_zdqy_xl = request.getParameterValues("ent_zdqy_xl[]");
            String zdqy = "";
            for (int i = 0; i < ent_zdqy_xl.length; i++) {
                if (i == (ent_zdqy_xl.length - 1)) {
                    zdqy += ent_zdqy_xl[i];
                } else {
                    zdqy += ent_zdqy_xl[i] + ",";
                }
            }
            pd.put("ent_zdqy_xl", zdqy);
        }

        return zsglService.insertSave(pd, parameterValues, request);
    }

    /**
     * 更新企业
     *
     * @param request
     * @return
     */
    @RequestMapping("/updateQiye")
    @ResponseBody
    public Object updateQiye(HttpServletRequest request) throws Exception {
        PageData pd = this.getPageData();
        pd.put("ent_state", "待认定");
        String[] parameterValues = request.getParameterValues("rdbimg[]");
        String rdb = "";
        if (parameterValues != null) {
            for (int i = 0; i < parameterValues.length; i++) {
                if (i == (parameterValues.length - 1)) {
                    rdb += parameterValues[i];
                } else {
                    rdb += parameterValues[i] + ",";
                }
            }
        }
        pd.put("rdbimg", rdb);


        // 获取重点企业类别并拼接
        if (request.getParameterValues("ent_zdqy_xl[]") != null) {
            String[] ent_zdqy_xl = request.getParameterValues("ent_zdqy_xl[]");
            String zdqy = "";
            for (int i = 0; i < ent_zdqy_xl.length; i++) {
                if (i == (ent_zdqy_xl.length - 1)) {
                    zdqy += ent_zdqy_xl[i];
                } else {
                    zdqy += ent_zdqy_xl[i] + ",";
                }
            }
            pd.put("ent_zdqy_xl", zdqy);
        }

        return zsglService.updateQiye(pd, parameterValues, request);
    }


    /**
     * 修改认定状态
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateState")
    @ResponseBody
    public Object updateState(HttpServletRequest request) throws Exception {
        PageData pd = this.getPageData();
        // 获取重点企业类别并拼接
        if (request.getParameterValues("ent_zdqy_xl[]") != null) {
            String[] ent_zdqy_xl = request.getParameterValues("ent_zdqy_xl[]");
            String zdqy = "";
            for (int i = 0; i < ent_zdqy_xl.length; i++) {
                if (i == (ent_zdqy_xl.length - 1)) {
                    zdqy += ent_zdqy_xl[i];
                } else {
                    zdqy += ent_zdqy_xl[i] + ",";
                }
            }
            pd.put("ent_zdqy_xl", zdqy);
        }
        return zsglService.updateState(pd);
    }


    /**
     * 获取产业主管部门指标数据
     *
     * @param request
     * @return
     */
    @RequestMapping("/getCyzgList")
    @ResponseBody
    public Object getCyzgList(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        PageData pd = this.getPageData();
        if ("".equals(pd.get("year"))) {
            pd.remove("year");
        }
        if ("".equals(pd.get("month"))) {
            pd.remove("month");
        }
        if (!"".equals(pd.get("month"))) {
            pd.put("y", pd.get("year"));
            pd.remove("year");
        }
        if ("".equals(pd.get("entname"))) {
            pd.remove("entname");
        }
        result.put("ZbList", zsglService.getCyzgList(pd));

        return result;
    }

    /**
     * 获取联合招商部门指标数据
     *
     * @param request
     * @return
     */
    @RequestMapping("/getLhzsList")
    @ResponseBody
    public Object getLhzsList(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        PageData pd = this.getPageData();
        if ("".equals(pd.get("year"))) {
            pd.remove("year");
        }
        if ("".equals(pd.get("month"))) {
            pd.remove("month");
        }
        if (!"".equals(pd.get("month"))) {
            pd.put("y", pd.get("year"));
            pd.remove("year");
        }
        if ("".equals(pd.get("entname"))) {
            pd.remove("entname");
        }
        result.put("ZbList", zsglService.getLhzsList(pd));
        return result;
    }

    /**
     * 获取指标考核对应部门引进企业
     *
     * @param request
     * @return
     */
    @RequestMapping("getZbkhlList")
    @ResponseBody
    public Object getZbkhlList(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        PageData pd = this.getPageData();
        int pageSize = Integer.parseInt(pd.get("pageSize").toString());
        int pageNo = Integer.parseInt(pd.get("pageNo").toString());

        Integer startPage = ((pageSize * (pageNo - 1)));
        Integer endPage = startPage + pageSize;

        pd.put("startPage", startPage.toString());
        pd.put("endPage", endPage.toString());

        if ("企业类型".equals(pd.get("ent_gjj"))) {
            pd.remove("ent_gjj");
        }
        if ("重点企业".equals(pd.get("ent_gjj"))) {
            pd.remove("ent_gjj");
            pd.put("ent_zdqy", "重点企业");
        }
        if ("外资企业".equals(pd.get("ent_gjj"))) {
            pd.remove("ent_gjj");
            pd.put("ent_sfwz", "是");
        }
        if ("加分项".equals(pd.get("pluses"))) {
            pd.remove("pluses");
        }
        if ("企业引进方式".equals(pd.get("ent_yjfs"))) {
            pd.remove("ent_yjfs");
        }
        if ("".equals(pd.get("entname"))) {
            pd.remove("entname");
        }
        result.put("num", zsglService.getzbkhNum(pd));
        result.put("List", zsglService.getZbkhlList(pd));
        return result;
    }


    /**
     * 更新指标加分
     *
     * @param request
     * @return
     */
    @RequestMapping("/updateJfxx")
    @ResponseBody
    public Object updateJfxx(HttpServletRequest request) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        PageData pd = this.getPageData();
        result.put("ZbList", zsglService.updateJfxx(pd));
        return result;
    }


    /**
     * 项目总览
     *
     * @param request
     * @return
     */
    @RequestMapping("/getXmzl")
    @ResponseBody
    public Object getXmzl(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        PageData pd = this.getPageData();
        int pageSize = Integer.parseInt(pd.get("pageSize").toString());
        int pageNo = Integer.parseInt(pd.get("pageNo").toString());
        Integer startPage = ((pageSize * (pageNo - 1)));
        Integer endPage = startPage + pageSize;

        pd.put("startPage", startPage.toString());

        if ("企业类型".equals(pd.get("ent_gjj"))) {
            pd.remove("ent_gjj");
        }
        if ("重点企业".equals(pd.get("ent_gjj"))) {
            pd.remove("ent_gjj");
            pd.put("ent_zdqy", "重点企业");
        }
        if ("外资企业".equals(pd.get("ent_gjj"))) {
            pd.remove("ent_gjj");
            pd.put("ent_sfwz", "是");
        }
        if ("信息提供单位".equals(pd.get("ent_xxtgdw"))) {
            pd.remove("ent_xxtgdw");
        }
        if ("企业引进单位".equals(pd.get("ent_yjqydw"))) {
            pd.remove("ent_yjqydw");
        }
        if ("认定状态".equals(pd.get("ent_state"))) {
            pd.remove("ent_state");
        }
        if ("".equals(pd.get("entname"))) {
            pd.remove("entname");
        }
        if ("".equals(pd.get("year"))) {
            pd.remove("year");
        }
        if ("".equals(pd.get("month"))) {
            pd.remove("month");
        }
        if (!"".equals(pd.get("month"))) {
            pd.put("y", pd.get("year"));
            pd.remove("year");
        }

        result.put("num", zsglService.getXmzlNum(pd));
        result.put("ZbList", zsglService.getXmzl(pd));
        return result;
    }


    /**
     * 项目统计
     *
     * @param request
     * @return
     */
    @RequestMapping("/getProject")
    @ResponseBody
    public Object getProject(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        PageData pd = this.getPageData();
        if ("".equals(pd.get("year"))) {
            pd.remove("year");
        }
        result.put("gjj", zsglService.getGjjListNum());
        result.put("zdqy", zsglService.getZdListNum());
        result.put("wzqy", zsglService.getWzListNum());
        result.put("cyzg", zsglService.getCyzgNum());
        result.put("lhzs", zsglService.getLhzsNum());
        result.put("ljss", zsglService.getSslj());
        return result;
    }




    @RequestMapping("/getDbxz")
    @ResponseBody
    public Object getDbxz(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String dbxz = zsglService.getDbxz(request);
        Map<String, String> result = new HashMap<>();
        result.put("down",dbxz);
        return result;
    }
}

